<?php
/*
 * @FilePath: \crm\application\crm\controller\Index.php
 */

namespace app\crm\controller;

use app\crm\model\CrmCustomer;
use NieGe\Redis;
use think\facade\Env;

class Demo extends BaseController
{
    public function test()
    {
        return $this->fetch();
    }

    public function create()
    {
        return $this->fetch();
    }

    public function upload()
    {
        $file = request()->file('file');
        $xlsUploads = Env::get('root_path') . '/public/temp';
        if( !file_exists($xlsUploads) ){
            mkdir($xlsUploads);
        }
        $info = $file->move($xlsUploads);

        return [
            'code' => 0,
            'msg' => '',
            'data' => [
                'src' => $info->getSaveName()
            ]
        ];
    }

    public function read()
    {
        $xlsFile = Env::get('root_path') . '/public/temp/20230303/d0a6a2a5a94c6aeaa1a7cc7dabae2403.xlsx';
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
        $reader->setReadDataOnly(TRUE);
        $spreadsheet = $reader->load($xlsFile); //载入excel表格

        $worksheet = $spreadsheet->getActiveSheet();
        $highestRow = $worksheet->getHighestRow(); // 总行数
        $highestColumn = $worksheet->getHighestColumn(); // 总列数

        if( $highestRow <= 1 ){
            exit('Excel表格中没有数据');
        }
        $data = [];
        for ($row = 2; $row <= $highestRow; ++$row) {
            $company = $worksheet->getCellByColumnAndRow(1, $row)->getValue(); 
            $model = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
            $number = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
            $master = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
            $phone = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
        
            $data[] = [
                'company' => $company,
                'model'   => $model,
                'number' => $number,
                'master' => $master,
                'phone' => $phone
            ];
        }

        dump($data);
    }

    public function a()
    {
        Redis::getInstance()->del('name');
        dump(Redis::getInstance()->setnx('name','lisi521'));
    }
}